import 'package:dio/dio.dart';
import 'package:flutter_menu/app_config.dart';
import 'package:flutter_menu/biz/order/record/shop_car_controller.dart';
import 'package:flutter_menu/data/menu_repository.dart';
import 'package:flutter_menu/data/provider/category_provider.dart';
import 'package:flutter_menu/data/provider/dept_provider.dart';
import 'package:flutter_menu/data/provider/electronic_provider.dart';
import 'package:flutter_menu/data/provider/formula_provider.dart';
import 'package:flutter_menu/data/provider/material_dept_stock_provider.dart';
import 'package:flutter_menu/data/provider/material_provider.dart';
import 'package:flutter_menu/data/provider/option_material_provider.dart';
import 'package:flutter_menu/data/provider/product_provider.dart';
import 'package:flutter_menu/data/provider/sell_helper.dart';
import 'package:flutter_menu/data/provider/sellout_provider.dart';
import 'package:flutter_menu/data/provider/shop_provider.dart';
import 'package:get/get.dart';

import 'biz/setting/setting_controller.dart';
import 'objectbox.g.dart';

class MenuBinding extends Bindings {
  @override
  void dependencies() async {
    Get.put(AppConfig.instance);
    Get.put(getDio());
    Get.put(MenuRepository.instance);
    Get.put(CategoryProvider(Get.find<Store>()));
    Get.put(ProductProvider(Get.find<Store>()));
    Get.put(ElectronicProvider(Get.find<Store>()));
    Get.put(FormulaProvider(Get.find<Store>()));
    Get.put(MaterialProvider(Get.find<Store>()));
    Get.put(MaterialDeptStockProvider(Get.find<Store>()));
    Get.put(SelloutProvider(Get.find<Store>()));
    Get.put(OptionMaterialProvider(Get.find<Store>()));
    Get.put(ShopProvider(Get.find<Store>()));
    Get.put(DeptProvider(Get.find<Store>()));
    Get.put(SellHelper());

    /// TODO remove in release mode
    Get.find<CategoryProvider>().putDemoData();
    Get.find<ProductProvider>().putDemoData();
    Get.find<ElectronicProvider>().putDemoData();

    Get.put(SettingController());
    Get.put(ShopCarController());
  }

  Dio getDio() {
    final Dio dio = Dio();
    dio.options = BaseOptions(
        receiveTimeout: const Duration(seconds: 45),
        connectTimeout: const Duration(seconds: 45));
    dio.interceptors.add(CommInterceptor());
    return dio;
  }
}

class CommInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    options.headers.addAll(AppConfig.instance.headers);
    super.onRequest(options, handler);
  }
}
